<template>
  <div>
    <div v-if="shows == 'list'">
      <heads />

      <Header>
        <el-button
          plain
          size="mini"
          type="primary"
          @click="
            shows = 'form';
            dataMsg = {};
            $emit('showTag', false);
          "
          >新建运费模板</el-button
        >
        <Alert :txt="'开启商家配送的商户需设置，最多可创建100个'" />
      </Header>
      <Main :type="'ordinary'" :footer-lock="totals > 10">
        <div v-for="(item, index) in listData" :key="index" class="table-new">
          <div class="table-new-head">
            <p>
              <span class="t-name">{{ item.name }}</span>
              <span v-if="item.is_default" style="color:#F59A23;"
                >新商品默认模板</span
              >
              <Alert
                style="width:400px;display: inline-block;margin-left: 20px;"
                :txt="
                  `使用该模板的商品：${item.nums}个  最后编辑时间：${item.update_time}`
                "
              />
            </p>

            <!-- <p>使用该模板的商品：{{ item.nums }}个</p>
          <p>最后编辑时间：{{ item.update_time }}</p> -->
            <p>
              <el-checkbox
                v-model="item.is_default"
                @change="raChange(item.templete_id, index)"
                ><span style="font-size:16px;">设为默认</span></el-checkbox
              >
            </p>
          </div>

          <Table
            :data="item.disarea"
            :span-method="objectOneMethod"
            style="margin-bottom:0;"
          >
            <el-table-column width="30" type="selection"> </el-table-column>
            <el-table-column
              label="可配送区域"
              align="center"
              property="area_name"
            ></el-table-column>

            <el-table-column
              label="首件（个）"
              align="center"
              property="first"
            ></el-table-column>
            <el-table-column
              label="运费（元）"
              align="center"
              property="first_price"
            ></el-table-column>
            <el-table-column
              label="续件（元）"
              align="center"
              property="continuation"
            ></el-table-column>
            <el-table-column
              label="续费（元）"
              align="center"
              property="continuation_price"
            ></el-table-column>

            <el-table-column
              label="操作"
              align="center"
              fixed="right"
              class-name="tableoperation"
            >
              <template>
                <el-button size="mini" type="text" @click="edit(item)">
                  编辑
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  @click="copy(item.templete_id)"
                >
                  复制
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  @click="del(item.templete_id)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </Table>
        </div>
      </Main>
      <Footer :total="totals">
        <el-pagination
          :page-size="5"
          layout="prev, pager, next"
          :total="totals"
          @current-change="changes"
        >
        </el-pagination>
      </Footer>
    </div>
    <merchant-form v-if="shows == 'form'" :data-msg="dataMsg" @out="out" />
  </div>
</template>
<script>
import {
  SETDEFAULTTEMPLETE,
  DISTRIBUTIONTEMPLETECOPY,
  DISTRIBUTIONTEMPLETELIST,
  DELDISTRIBUTIONTEMPLETE
} from "../../service";
import heads from "../component/heads";
export default {
  components: {
    merchantForm: () => import("./form"),
    heads
  },
  data() {
    return {
      listData: [],
      totals: 0,
      pages: 0,
      shows: "list",
      privilege: "",
      card_id: "",
      editCardId: "",
      send: false,
      options: "first",
      idx: "",
      dataMsg: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async raChange(templeteId, index) {
      this.listData[index].is_default = false;
      await SETDEFAULTTEMPLETE({ templete_id: templeteId }).then(res => {
        let { code, msg } = res;
        if (code === 200) {
          this.$message.success(msg);
          this.getList();
        } else {
          this.$message.error(msg);
        }
      });
    },
    out() {
      this.shows = "list";
      this.$emit("showTag", true);
      this.getList();
    },

    edit(item) {
      this.dataMsg = item;
      this.shows = "form";
      this.$emit("showTag", false);
    },
    async copy(templeteId) {
      await DISTRIBUTIONTEMPLETECOPY({ templete_id: templeteId }).then(res => {
        let { code, msg } = res;
        if (code === 200) {
          this.$message.success(msg);
          this.getList();
        } else {
          this.$message.error(msg);
        }
      });
    },
    async del(templeteId) {
      await this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          DELDISTRIBUTIONTEMPLETE({ templete_id: templeteId }).then(res => {
            let { code, msg } = res;
            if (code === 200) {
              this.getList();
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除!"
          });
        });
    },
    async getList() {
      await DISTRIBUTIONTEMPLETELIST({ page: this.pages, pageshow: 5 }).then(
        ({ code, data, msg }) => {
          if (code === 200) {
            this.totals = data.count;
            data.list.forEach(ele => {
              if (ele.planJson) {
                ele.planJson = JSON.parse(ele.planJson);
                ele.disarea.unshift(ele.planJson);
              }
              ele.is_default = ele.is_default === 1;
            });

            this.listData = data.list;
          } else {
            this.$message.error(msg);
            this.listData = [];
          }
        }
      );
    },
    changes(page) {
      this.pages = page;
      this.getList();
    },

    setTable(tableData) {
      let spanOneArr = [];
      tableData.forEach(item => {
        spanOneArr.push(item.disarea.length);
      });
      return spanOneArr;
    },

    objectOneMethod({ columnIndex }) {
      if (columnIndex === 6) {
        // const _row = this.setTable(this.listData)[rowIndex]
        return {
          rowspan: 35,
          colspan: 1
        };
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.tabs {
  width: 154px;
  margin-left: 60px;
}
.news {
  height: 80px;
  text-align: left;
  line-height: 70px;
  padding-left: 20px;
  // border-bottom: 10px solid #fafafa;
  span {
    font-size: 14px;
    color: #aaaaaa;
  }
}
.table-new {
  border-bottom: 10px solid #f2f2f2;
  overflow: hidden;
  .table-new-head {
    width: 100%;
    height: 60px;
    line-height: 60px;
    padding-left: 20px;
    border-bottom: 1px solid #f2f2f2;
    box-sizing: border-box;
    p {
      float: left;
      text-align: left;
      &:nth-child(1) {
        width: 82%;
        .t-name {
          display: inline-block;
          font-weight: bold;
          margin-right: 20px;
        }
      }
      &:nth-child(2) {
        width: 14%;
        text-align: right;
        padding-right: 4%;
      }
    }
  }
}
</style>
